/**
 * Copyright (c) 2009 Cerner Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cerner Corporation - initial API and implementation
 */
package com.cerner.system.rest.jaas.cas.functional;

import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;

/**
 * Simple stub implementation of a centralized authentication service that
 * creates {@code TGT}s and hands out {@code ST}s.
 * <ul>
 * <li>Ticket Granting Tickets can be generated with {@code POST}s to the URL
 * <code>https://localhost:{PORT}/cas/</code>.</li>
 * <li>Service Tickets, in turn, can be obtained via {@code POST}s to
 * <code>https://localhost:{PORT}/cas/{TGT}</code>.</li>
 * <li>Everything else results in an Internal Server Error.</li>
 * </ul>
 * 
 * @author Alex Horn
 */
public class StubCasRestlet extends Restlet {

  @Override
  public synchronized void handle(Request request, Response response) {
    super.handle(request, response);

    final String requestUrl = request.getResourceRef().toString();
    if (!org.restlet.data.Method.POST.equals(request.getMethod())) {
      response.setStatus(Status.SERVER_ERROR_INTERNAL,
          "Only POSTs are supported.");
    } else if (requestUrl.endsWith("/cas/")) {
      response.setLocationRef("some/redirect/url/TGT-fc5a-R");
      response.setStatus(Status.SUCCESS_CREATED);
    } else if (requestUrl.endsWith("/cas/TGT-fc5a-R")) {
      response.setEntity("ST-1", MediaType.TEXT_ALL);
    } else {
      response.setStatus(Status.SERVER_ERROR_INTERNAL,
          "Invalid Request: ".concat(request.getResourceRef().toString()));
    }
  }
}
